package com.ljh.lottery.service.activitystatus.operator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ljh.lottery.mapper.ActivityPrizeMapper;
import com.ljh.lottery.pojo.dataobject.ActivityPrizeDO;
import com.ljh.lottery.service.dto.ConvertActivityStatusDTO;
import com.ljh.lottery.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: Themberfue
 * @date: 2025/4/17 10:40
 * @description:
 */
@Component
public class PrizeOperator extends AbstractActivityOperator {
    @Autowired
    ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        // TODO 1. 校验参数
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityPrizeStatusEnum activityPrizeStatusEnum = convertActivityStatusDTO.getTargetPrizeStatus();
        if (null == activityId
                || null == prizeId
                || null == activityPrizeStatusEnum) {
            return false;
        }

        // TODO 2. 检验其是否需要扭转状态
        QueryWrapper<ActivityPrizeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ActivityPrizeDO::getActivityId, activityId)
                .eq(ActivityPrizeDO::getPrizeId, prizeId);
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectOne(queryWrapper);
        if (null == activityPrizeDO) {
            return false;
        }
        return !activityPrizeDO.getStatus()
                .equalsIgnoreCase(activityPrizeStatusEnum.name());
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            UpdateWrapper<ActivityPrizeDO> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .set(ActivityPrizeDO::getStatus, convertActivityStatusDTO.getTargetPrizeStatus().name())
                    .eq(ActivityPrizeDO::getActivityId, convertActivityStatusDTO.getActivityId())
                    .eq(ActivityPrizeDO::getPrizeId, convertActivityStatusDTO.getPrizeId());
            activityPrizeMapper.update(updateWrapper);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
